﻿namespace Cmn.Mis
{
    using Cmn;
    using System;
    using System.Drawing;
    using System.Web.UI.WebControls;

    public enum FilterOpt
    {
        包含,
        等于,
        不等于,
        不包含
    }

    public class RecFilter : Panel
    {
        private DropDownList ddlCols = new DropDownList();
        private DropDownList ddlRela = new DropDownList();
        private Label lblOne = new Label();
        private Label lblTwo = new Label();
        private RadioButtonList rblSearchRange = new RadioButtonList();
        private RadioButtonList rblSearchType = new RadioButtonList();
        private TextBox txtValue = new TextBox();
        private TextBox txtValue1 = new TextBox();

        public RecFilter()
        {
            this.BackColor = Color.FromArgb(0xd2d9fb);
            this.rblSearchType.Items.Add("单值");
            this.rblSearchType.Items.Add("区间");
            this.rblSearchType.SelectedIndex = 0;
            this.rblSearchType.RepeatDirection = RepeatDirection.Horizontal;
            this.rblSearchType.RepeatLayout = RepeatLayout.Flow;
            this.rblSearchType.SelectedIndexChanged += new EventHandler(this.rblSearchType_SelectedIndexChanged);
            this.rblSearchType.AutoPostBack = true;
            this.Controls.Add(this.rblSearchType);
            this.Controls.Add(UI.GetBlank(10));
            this.rblSearchRange.Items.Add("在结果中查询");
            this.rblSearchRange.Items.Add("重新查询");
            this.rblSearchRange.RepeatDirection = RepeatDirection.Horizontal;
            this.rblSearchRange.RepeatLayout = RepeatLayout.Flow;
            this.rblSearchRange.SelectedIndex = 1;
            this.Controls.Add(this.rblSearchRange);
            this.Controls.Add(UI.GetBR(1));
            this.Controls.Add(UI.GetBlank(1));
            Label child = new Label {
                Text = "字段名：",
                ForeColor = Color.Blue
            };
            this.Controls.Add(child);
            this.Controls.Add(this.ddlCols);
            this.lblOne.Text = " >= ";
            this.lblOne.ForeColor = Color.Blue;
            this.lblOne.Font.Bold = true;
            this.lblOne.Visible = false;
            this.Controls.Add(this.lblOne);
            this.txtValue.Visible = false;
            this.Controls.Add(this.txtValue);
            this.ddlRela.Items.Add(" "+FilterOpt.包含+" ");
            this.ddlRela.Items.Add(" " + FilterOpt.等于 + " ");
            this.ddlRela.Items.Add(" " + FilterOpt.不等于 + " ");
            this.ddlRela.Items.Add(" " + FilterOpt.不包含 + " ");
            this.ddlRela.Font.Bold = true;
            this.ddlRela.ForeColor = Color.Blue;
            this.Controls.Add(this.ddlRela);
            this.lblTwo.Text = " <= ";
            this.lblTwo.Visible = false;
            this.lblTwo.ForeColor = Color.Blue;
            this.lblTwo.Font.Bold = true;
            this.Controls.Add(this.lblTwo);
            this.Controls.Add(this.txtValue1);
        }

        public void AddCol(string colName, string colTitle)
        {
            this.ddlCols.Items.Add(new ListItem(colTitle, colName));
        }

        public string GetFilterSql(string initSql, string curSql)
        {
            string str5;
            string where = "";
            string fullFieldName = SqlAnalyse.GetFullFieldName(initSql, this.ddlCols.SelectedValue);
            this.txtValue.Text = Safe.sqlStr(this.txtValue.Text);
            this.txtValue1.Text = Safe.sqlStr(this.txtValue1.Text);
            if (this.rblSearchType.SelectedIndex != 0)
            {
                if (this.txtValue.Text.Trim() != "")
                {
                    str5 = where;
                    where = str5 + fullFieldName + ">='" + this.txtValue.Text + "'";
                }
                if (this.txtValue1.Text.Trim() != "")
                {
                    if (where != "")
                    {
                        if (this.ddlRela.SelectedIndex == 0)
                        {
                            where = where + " and ";
                        }
                        else
                        {
                            where = where + " or ";
                        }
                    }
                    str5 = where;
                    where = str5 + fullFieldName + "<='" + this.txtValue1.Text + "'";
                }
            }
            else
            {
                switch (this.ddlRela.SelectedIndex)
                {
                    case 0:
                        if (this.txtValue1.Text != "")
                        {
                            str5 = where;
                            where = str5 + fullFieldName + " like '%" + this.txtValue1.Text + "%'";
                        }
                        goto Label_03E2;

                    case 1:
                        if (!(this.txtValue1.Text != ""))
                        {
                            str5 = where;
                            where = str5 + fullFieldName + " ='' or " + fullFieldName + " is null";
                        }
                        else
                        {
                            str5 = where;
                            where = str5 + fullFieldName + " ='" + this.txtValue1.Text + "'";
                        }
                        goto Label_03E2;

                    case 2:
                        if (!(this.txtValue1.Text != ""))
                        {
                            str5 = where;
                            where = str5 + fullFieldName + " <>'' or " + fullFieldName + " is not null";
                        }
                        else
                        {
                            str5 = where;
                            where = str5 + fullFieldName + " <>'" + this.txtValue1.Text + "' or " + fullFieldName + " is null ";
                        }
                        goto Label_03E2;

                    case 3:
                        if (!(this.txtValue1.Text != ""))
                        {
                            where = where + " 1=0 ";
                        }
                        else
                        {
                            str5 = where;
                            where = str5 + fullFieldName + " not like '%" + this.txtValue1.Text + "%' or " + fullFieldName + " is null ";
                        }
                        goto Label_03E2;
                }
            }
        Label_03E2:
            if (this.rblSearchRange.SelectedIndex == 0)
            {
                return SqlAnalyse.AddWhere(curSql, where);
            }
            return SqlAnalyse.AddWhere(initSql, where);
        }

        private void rblSearchType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.rblSearchType.SelectedIndex == 0)
            {
                this.txtValue.Visible = false;
                this.lblOne.Visible = false;
                this.lblTwo.Visible = false;
                this.ddlRela.Items.Clear();
                this.ddlRela.Items.Add(" " + FilterOpt.包含 + " ");
                this.ddlRela.Items.Add(" " + FilterOpt.等于 + " ");
                this.ddlRela.Items.Add(" " + FilterOpt.不等于 + " ");
                this.ddlRela.Items.Add(" " + FilterOpt.不包含 + " ");
                this.ddlRela.Font.Bold = true;
            }
            else
            {
                this.txtValue.Visible = true;
                this.lblOne.Visible = true;
                this.lblTwo.Visible = true;
                this.ddlRela.Items.Clear();
                this.ddlRela.Items.Add("并且");
                this.ddlRela.Items.Add("或");
                this.ddlRela.Font.Bold = false;
            }
        }
    }
}

